Suppose we have a line of code or a bunch of lines
of code that we'd like to make run more than once.
You could just write those lines of code
and copy and paste them so that you add them again,
but that would start to give you long programs that were hard to understand.
So, Python gives us a special command that says do some lines of code multiple times.
It's called a for loop or iteration.
We begin with a special word for.
Then we have some variable name or in this case,
since we're not really using it,
we just have underscore,
and we have a special word in.
In this case, we say do it three times, and then we have a colon.
All of the lines of code that are indented farther in,
in this case, by a few spaces,
all of those lines of code are a block,
and that block of code will be executed three times in this case.
It's always a good idea to work on your understanding of
code before you run it to try to predict what it will do.
In this case, you might imagine that it's going to print.
In our output window, this will execute first,
and then it'll say this will execute three times.
This line will execute three times.
Then this line will also execute three times.
This line will also execute three times.
In fact, it doesn't do that.
It doesn't print this line three times and then this one three times.
What it does is it executes line four and then nine
five and then four and then five and then four and then five.
So, the whole block is executed,
and then the whole block is executed again,
and the whole blocks is executed a third time.
Let's see how that works.
So, you can see in our output window,
the whole block executes once,
and the whole block executes a second time,
and the whole block executes a third time.
Let's go look at the next example.
In this next example,
we're going to be able to draw
a more interesting turtle program by
having a set of commands get executed a bunch of times.
In this case, we're creating a turtle named elan.
We're going to make elan do some commands 10 times.
For underscore in range of 10,
do this block from lines eight through 10.
Do that 10 times.
So, elan is going to go forward by 50 and then turn right 90.
Remember, from the previous chapter,
what distance equals distance plus 10 does.
We had the variable distance,
and it was initially bound to the value 50.
We look up 50 plus 10,
that equals 60, and we replace the 50 with 60.
The next time that we execute line 10,
we're going to replace it with 70.
You can see that that distance variable is used here to determine how far elan will move.
So, each time elan moves forward,
he's going to go forward a little bit farther,
50 pixels the first time,
then 60, then 70.
So, before I run this,
you might want to pause the recording and see if you can envision in
your mind what is this going to look like when I run it.
Here we go. Let's see.
So, it's getting a little bit longer each time.
That means instead of just making a square,
it ends up making a spiral.
So, how many times does it do this block?
It does it 10 times because it says range of 10 there.
Now, what if I made the angle increase every time, too?
Suppose instead of turning right by 90 degrees,
I turned right by whatever the current value of the angle was,
and it started at 90.
If I do this, I get just exactly the same picture that I have right now.
But suppose that I change the angle every time.
Maybe I'll make it a little bit smaller.
So, again, you might want to pause the video and see if you can
predict what this is going to look like. Here we go.
So, the angle is getting a little smaller each time.
So, we end up with this even more interesting spiral.
Now, if I wanted the spiral to go even farther,
I could increase this range.
This means it's going to go forward and turn more times.
He's a little off the screen there, and now he's back.
Maybe he's gone forever now.
So, that's making the turtle.
Do a set of commands a bunch of times so that we can have relatively short program,
but have it do a lot of drawing on the screen.